clear all
set more off
set graphics off
capture log close
set maxvar 20000
 

global dates_coefplot_mobility "_21980 _21981 _21982 _21983 _21984 _21985 _21986 _21987 _21988 _21989 _21990 _21991 _21992 _21993 _21994 _21995 _21996 _21997 _21998 _21999 _22000 _22001 _22002 _22003 _22004 _22005 _22006 _22007 _22008 _22009 _22010 _22011 _22012 _22013 _22014 _22015 _22016 _22017 _22018 _22019"
global dates_coefplot_mobility_sf "_21946 _21947 _21948 _21949 _21950 _21951 _21952 _21953 _21954 _21955 _21956 _21957 _21958 _21959 _21960 _21961 _21962 _21963 _21964 _21965 _21966 _21967 _21968 _21969 _21970 _21971 _21972 _21973 _21974 _21975 _21976 _21977 _21978 _21979 _21980 _21981 _21982 _21983 _21984 _21985 _21986 _21987 _21988 _21989 _21990 _21991 _21992 _21993 _21994 _21995 _21996 _21997 _21998 _21999 _22000 _22001 _22002 _22003 _22004 _22005 _22006 _22007 _22008 _22009 _22010 _22011 _22012 _22013 _22014 _22015 _22016 _22017 _22018 _22019 _22020 _22021 _22022 _22023 _22024 _22025 _22026 _22027 _22028 _22029 _22030 _22031 _22032 _22033 _22034 _22035"

global outcomes_pick4 "tot_expgroc tot_expcovid_prod"
global outcomes_pick2 "sd_sf_dist_fr_home sd_sf_median_nohome_time sd_sf_pt_work_dev sd_sf_ft_work_dev sd_sf_home_dev_count"
global outcomes_pick1 "ratio_cases_pop ratio_deaths_pop"

global other_ch_fxnc "msnbc cnn"
global other_ch_cnn "msnbc fxnc"
global other_ch_msnbc "fxnc cnn"
	
replace fxnc=-fxnc
replace cnn=-cnn
replace msnbc=-msnbc

gen fxnc_rtg=rtgxxfxnc_2020
gen cnn_rtg=rtgxxcnn_2020
gen msnbc_rtg=rtgxxmsnbc_2020

drop pop
rename county_fips fips_county
merge n:1 fips_county using /Users/matteop/Dropbox/final_matteo/corona_virus/definitive_data/pop_county.dta
drop if _merge==2
drop _merge
rename pop pop_county
rename pop_zipcode pop
rename fips_county county_fips

gen fxnc_rtg_w=fxnc_rtg*pop_county/pop
gen cnn_rtg_w=cnn_rtg*pop_county/pop
gen msnbc_rtg_w=msnbc_rtg*pop_county/pop

global full_controls1 "pop_dens landarea age_imb pct_food_stamps pct_nohs pct_college med_white med_black pct_male"

save temporary_prepped.dta, replace

use temporary_prepped.dta, clear


* REGRESSIONS
global regressions=1
if ${regressions}==1{
	foreach y in tot_expgroc {
	eststo clear
		forvalues date=21946/22035 {
		cap qui eststo: reghdfe sd_`y'_w_`date' fxnc [aweight=pop], absorb(state_fips one##c.(sd_`y'_j20 $full_controls1)) cluster(state_fips)
		}
	esttab using "analysis/data_alldates/channels_2016/paper_output_full/reduced_form/2016_janavg_fxnc_censuscontrols1_state_fips_`y'.csv", unstack ar2 se replace  star(+ 0.10 * 0.05 ** 0.01 @ 0.001)
	}
	
* GENERAL I CHECK	
eststo clear	
preserve
foreach var in pop_dens pct_food_stamps pct_occ_serv	landarea pct_nohs  pct_college med_white med_black pct_male {
egen sd_`var'=sd(`var')
replace `var'=`var'/sd_`var'
}

	foreach fe in county_fips {
		foreach var in fxnc {
		* Checks on Instrument
		cap qui eststo:reghdfe age_imb `var' pop_dens   pct_food_stamps landarea pct_nohs  	pct_college med_white med_black pct_male [aweight=pop] , absorb(`fe') cluster(state_fips) nocons 

		cap qui eststo:reghdfe pct_food_stamps `var'  pop_dens age_imb pct_nohs  landarea	pct_college med_white med_black pct_male   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons 

		cap qui eststo:reghdfe pct_college `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs  med_white med_black pct_male [aweight=pop] , absorb(`fe') cluster(state_fips) nocons 

		cap qui eststo:reghdfe pct_male `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs pct_college med_white med_black   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons

		cap qui eststo:reghdfe pct_nohs `var'  pop_dens age_imb pct_food_stamps landarea  pct_college med_white med_black pct_male   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons 

		cap qui eststo:reghdfe med_black `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs pct_college med_white pct_male   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons

		cap qui eststo:reghdfe med_white `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs pct_college med_black pct_male   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons 

		cap qui eststo:reghdfe pop_dens `var'   age_imb pct_food_stamps landarea 	 pct_nohs  pct_college med_white med_black pct_male   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons
		
		cap qui eststo:reghdfe landarea  `var'   pop_dens age_imb pct_food_stamps pct_nohs pct_college med_white med_black pct_male   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons 
		}
	}
esttab using "analysis/data_alldates/channels_2016/paper_output_full/instrument_balancecheck_zipcode_countyfe.csv", ar2 se replace  star(* 0.05 ** 0.01)	
restore	

* PURCHASES	I CHECK
eststo clear
preserve
foreach var in pop_dens pct_food_stamps pct_occ_serv	landarea pct_nohs  pct_college med_white med_black pct_male {
egen sd_`var'=sd(`var')
replace `var'=`var'/sd_`var'
}

	foreach fe in state_fips  {
		foreach var in fxnc {
		* Checks on Instrument
		cap qui eststo:reghdfe age_imb `var' pop_dens   pct_food_stamps landarea pct_nohs  	pct_college med_white med_black pct_male if zipcode_in_sample==1 [aweight=pop] , absorb(county_fips) cluster(state_fips) nocons

		cap qui eststo:reghdfe pct_food_stamps `var'  pop_dens age_imb pct_nohs  landarea	pct_college med_white med_black pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons

		cap qui eststo:reghdfe pct_college `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs  med_white med_black pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons

		cap qui eststo:reghdfe pct_male `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs pct_college med_white med_black if zipcode_in_sample==1   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons 

		cap qui eststo:reghdfe pct_nohs `var'  pop_dens age_imb pct_food_stamps landarea  pct_college med_white med_black pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons 

		cap qui eststo:reghdfe med_black `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs pct_college med_white pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons 

		cap qui eststo:reghdfe med_white `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs pct_college med_black pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons 

		cap qui eststo:reghdfe pop_dens `var'   age_imb pct_food_stamps landarea 	 pct_nohs  pct_college med_white med_black pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons 

		cap qui eststo:reghdfe landarea  `var'   pop_dens age_imb pct_food_stamps pct_nohs pct_college med_white med_black pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons 

		}
	}
esttab using "analysis/data_alldates/channels_2016/paper_output_full/instrument_balancecheck_zipcode_purrchases.csv", ar2 se replace  star(* 0.05 ** 0.01)	

restore

}
*



* decadata sd weekly january baseline *
foreach pre in sd_ {
	foreach cl in state_fips {
		foreach network in fxnc {

		* Main outcomes REDUCED
				foreach y of global outcomes_pick4  {
				use temporary_prepped.dta, clear
					cap drop _est*				
						forvalues date=21946/22035 {
						cap qui reghdfe `pre'`y'_w_`date' `network' [aweight=pop], absorb(state_fips one##c.(sd_`y'_j20 $full_controls1)) cluster(`cl')
						}
					}	
		*
		* Main outcomes REDUCED
				foreach y of global outcomes_pick4  {
				use temporary_prepped.dta, clear
					cap drop _est*				
						forvalues date=21946/22035 {
						cap qui reghdfe `pre'`y'_w_`date' `network' [aweight=pop], absorb(county_fips one##c.(sd_`y'_j20 $full_controls1)) cluster(`cl')
						}
					}	
		*
		}
	}
}
*

* safegraph no baseline*
foreach cl in state_fips {
	foreach network in fxnc {

	* Main outcomes REDUCED
			foreach y of global outcomes_pick2 {
				cap drop _est*				
					forvalues date=21946/22035 {
					if inlist("`y'", "cases","lcases","ratio_cases_pop","gr_cases_dayly","gr_cases_county_nyt") {
					global time_case "days_since_1_case`date'"
					}
					if inlist("`y'","deaths","ldeaths","ratio_deaths_pop","gr_deaths_dayly","gr_death_county_nyt") {
					global time_death "days_since_1_death`date'"
					}	
					if dow`date'[1]==0{
					global baseline "`y'_sun"
					}					
					if dow`date'[1]==1{
					global baseline "`y'_mon"
					}					
					if dow`date'[1]==2{
					global baseline "`y'_tue"
					}					
					if dow`date'[1]==3{
					global baseline "`y'_wed"
					}					
					if dow`date'[1]==4{
					global baseline "`y'_thu"
					}					
					if dow`date'[1]==5{
					global baseline "`y'_fri"
					}					
					if dow`date'[1]==6{
					global baseline "`y'_sat"
					}

					cap qui reghdfe `y'`date' `network' [aweight=pop], absorb(state_fips one##c.($full_controls1)) cluster(`cl')
					}
				}	
	*

	* Main outcomes REDUCED
			foreach y of global outcomes_pick2 {
				cap drop _est*				
					forvalues date=21946/22035 {
					if dow`date'[1]==0{
					global baseline "`y'_sun"
					}					
					if dow`date'[1]==1{
					global baseline "`y'_mon"
					}					
					if dow`date'[1]==2{
					global baseline "`y'_tue"
					}					
					if dow`date'[1]==3{
					global baseline "`y'_wed"
					}					
					if dow`date'[1]==4{
					global baseline "`y'_thu"
					}					
					if dow`date'[1]==5{
					global baseline "`y'_fri"
					}					
					if dow`date'[1]==6{
					global baseline "`y'_sat"
					}

					cap qui reghdfe `y'`date' `network' [aweight=pop], absorb(county_fips one##c.($full_controls1)) cluster(`cl')
					}
				}	
	*
	}
}
*	

* safegraph *
foreach cl in state_fips {
	foreach network in fxnc {

	* Main outcomes REDUCED
			foreach y of global outcomes_pick2 {
				cap drop _est*				
					forvalues date=21946/22035 {
					if dow`date'[1]==0{
					global baseline "`y'_sun"
					}					
					if dow`date'[1]==1{
					global baseline "`y'_mon"
					}					
					if dow`date'[1]==2{
					global baseline "`y'_tue"
					}					
					if dow`date'[1]==3{
					global baseline "`y'_wed"
					}					
					if dow`date'[1]==4{
					global baseline "`y'_thu"
					}					
					if dow`date'[1]==5{
					global baseline "`y'_fri"
					}					
					if dow`date'[1]==6{
					global baseline "`y'_sat"
					}

					cap qui eststo: reghdfe `y'`date' `network' [aweight=pop], absorb(state_fips one##c.($baseline $full_controls1)) cluster(`cl')
					cap eststo _`date'
					macro drop baseline
					}
				}	
	*

	* Main outcomes REDUCED
			foreach y of global outcomes_pick2 {
				cap drop _est*				
					forvalues date=21946/22035 {
					if dow`date'[1]==0{
					global baseline "`y'_sun"
					}					
					if dow`date'[1]==1{
					global baseline "`y'_mon"
					}					
					if dow`date'[1]==2{
					global baseline "`y'_tue"
					}					
					if dow`date'[1]==3{
					global baseline "`y'_wed"
					}					
					if dow`date'[1]==4{
					global baseline "`y'_thu"
					}					
					if dow`date'[1]==5{
					global baseline "`y'_fri"
					}					
					if dow`date'[1]==6{
					global baseline "`y'_sat"
					}

					cap qui reghdfe `y'`date' `network' [aweight=pop], absorb(county_fips one##c.($baseline $full_controls1 $time_death $time_case)) cluster(`cl')
					macro drop baseline
					}

				}	
	*
	}
}
*	

* GENERAL I CHECK		
preserve
foreach var in pop_dens pct_food_stamps pct_occ_serv	landarea pct_nohs  pct_college med_white med_black pct_male {
egen sd_`var'=sd(`var')
replace `var'=`var'/sd_`var'
}
gen pop_dens_graph=pop_dens/10 

	foreach fe in state_fips county_fips {
		foreach var in fxnc {
		* Checks on Instrument
		ivreghdfe age_imb `var' pop_dens   pct_food_stamps landarea pct_nohs  	pct_college med_white med_black pct_male [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_age_imb

		ivreghdfe pct_food_stamps `var'  pop_dens age_imb pct_nohs  landarea	pct_college med_white med_black pct_male   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_pct_food_stamps

		ivreghdfe pct_college `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs  med_white med_black pct_male [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_pct_college

		ivreghdfe pct_male `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs pct_college med_white med_black   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_pct_male

		ivreghdfe pct_nohs `var'  pop_dens age_imb pct_food_stamps landarea  pct_college med_white med_black pct_male   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_pct_nohs

		ivreghdfe med_black `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs pct_college med_white pct_male   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_black

		ivreghdfe med_white `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs pct_college med_black pct_male   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_white

		ivreghdfe pop_dens_graph `var'   age_imb pct_food_stamps landarea 	 pct_nohs  pct_college med_white med_black pct_male   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_pop_dens_graph

		ivreghdfe landarea  `var'   pop_dens age_imb pct_food_stamps pct_nohs pct_college med_white med_black pct_male   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_landarea

		coefplot (coef_pop_dens_graph, aseq(Pop Density)) (coef_age_imb, aseq(Age Imbalance)) (coef_pct_food_stamps, aseq(Food Stamps pct)) (coef_pct_college, aseq(College pct)) (coef_pct_male, aseq(Male pct)) (coef_pct_nohs, aseq(No High School pct)) (coef_black, aseq(Black pop ab. median)) (coef_white, aseq(White pop ab. median)) (coef_landarea, aseq(Land Area)),  play(y_title_coefplot_no_legenda) graphregion(fcolor(white)) legend(off) ytitle("Covariate Balance Check on Channel Position Instrument") xtitle("Coefficient on Z with 95% CI’s") xline(0, lpattern(dash) lcolor(gray))  ciopts(color(teal))  symbol(circle_hollow) msize(med) color(maroon) keep(fxnc)  aseq swapnames byopts(xrescale)
		graph export "analysis/data_alldates/channels_2016/paper_output_full/zip_`fe'_instrument_checks_`var'_censuscontrols3_state_fips.pdf", replace
		}
	}
restore

* PURCHASES	I CHECK
preserve
foreach var in pop_dens pct_food_stamps pct_occ_serv	landarea pct_nohs  pct_college med_white med_black pct_male {
egen sd_`var'=sd(`var')
replace `var'=`var'/sd_`var'
}
gen pop_dens_graph=pop_dens

	foreach fe in state_fips county_fips {
		foreach var in fxnc {
		* Checks on Instrument
		ivreghdfe age_imb `var' pop_dens   pct_food_stamps landarea pct_nohs  	pct_college med_white med_black pct_male if zipcode_in_sample==1 [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_age_imb

		ivreghdfe pct_food_stamps `var'  pop_dens age_imb pct_nohs  landarea	pct_college med_white med_black pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_pct_food_stamps

		ivreghdfe pct_college `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs  med_white med_black pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_pct_college

		ivreghdfe pct_male `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs pct_college med_white med_black if zipcode_in_sample==1   [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_pct_male

		ivreghdfe pct_nohs `var'  pop_dens age_imb pct_food_stamps landarea  pct_college med_white med_black pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_pct_nohs

		ivreghdfe med_black `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs pct_college med_white pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_black

		ivreghdfe med_white `var'  pop_dens age_imb pct_food_stamps landarea pct_nohs pct_college med_black pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_white

		ivreghdfe pop_dens_graph `var'   age_imb pct_food_stamps landarea 	 pct_nohs  pct_college med_white med_black pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_pop_dens_graph

		ivreghdfe landarea  `var'   pop_dens age_imb pct_food_stamps pct_nohs pct_college med_white med_black pct_male if zipcode_in_sample==1  [aweight=pop] , absorb(`fe') cluster(state_fips) nocons first
		cap eststo coef_landarea

		coefplot (coef_pop_dens_graph, aseq(Pop Density)) (coef_age_imb, aseq(Age Imbalance)) (coef_pct_food_stamps, aseq(Food Stamps pct)) (coef_pct_college, aseq(College pct)) (coef_pct_male, aseq(Male pct)) (coef_pct_nohs, aseq(No High School pct)) (coef_black, aseq(Black pop ab. median)) (coef_white, aseq(White pop ab. median)) (coef_landarea, aseq(Land Area)),  play(y_title_coefplot_no_legenda) graphregion(fcolor(white)) legend(off) ytitle("Covariate Balance Check on Channel Position Instrument") xtitle("Coefficient on Z with 95% CI’s") xline(0, lpattern(dash) lcolor(gray))  ciopts(color(teal))  symbol(circle_hollow) msize(med) color(maroon) keep(fxnc)  aseq swapnames byopts(xrescale)
		graph export "analysis/data_alldates/channels_2016/paper_output_full/azip_purchases_`fe'_instrument_checks_`var'_censuscontrols3_state_fips.pdf", replace
		}
	}
restore


* RC POLYNOMIALS AND INTERACTIONS MAIN SAMPLE
global rc_polynomials_interactions=1
if ${rc_polynomials_interactions}==1 {


foreach check_var in landarea age_imb pct_nohs pct_male {
cap gen `check_var'_2=`check_var'^2	
cap gen `check_var'_3=`check_var'^3	
cap gen `check_var'_fxnc=`check_var'*fxnc
}

global full_controls_var1 "age_imb_2 age_imb_3"
global full_controls_var2 "landarea_2 landarea_3"
global full_controls_var3 "pct_male_2 pct_male_3"
global full_controls_var4 "pct_nohs_2 pct_nohs_3"
global full_controls_var5 "age_imb_fxnc"
global full_controls_var6 "landarea_fxnc"
global full_controls_var7 "pct_male_fxnc"
global full_controls_var8 "pct_nohs_fxnc"

global outcomes_pick "sd_sf_dist_fr_home sd_sf_median_nohome_time"

* RF COUNTY FE*
foreach cl in state_fips {
	* Main outcomes RF

			foreach y of global outcomes_pick {
				foreach controls_set in full_controls_var1 full_controls_var2 full_controls_var3 full_controls_var4 full_controls_var5 full_controls_var6 full_controls_var7 full_controls_var8 {				
					forvalues date=21946/22035 {
					if dow`date'[1]==0{
					global baseline "`y'_sun"
					}					
					if dow`date'[1]==1{
					global baseline "`y'_mon"
					}					
					if dow`date'[1]==2{
					global baseline "`y'_tue"
					}					
					if dow`date'[1]==3{
					global baseline "`y'_wed"
					}					
					if dow`date'[1]==4{
					global baseline "`y'_thu"
					}					
					if dow`date'[1]==5{
					global baseline "`y'_fri"
					}					
					if dow`date'[1]==6{
					global baseline "`y'_sat"
					}
					cap qui reghdfe `y'`date' fxnc [aweight=pop], absorb(county_fips one##c.($baseline $full_controls1 ${`controls_set'})) cluster(`cl')
					macro drop baseline
					}
			}
		*
		}
*
}
*
}
*

* RC POLYNOMIALS PURCHASES SAMPLE
global rc_polynomials=1
if ${rc_polynomials}==1 {

foreach check_var in landarea age_imb pct_nohs pct_male {
cap gen `check_var'_2=`check_var'^2	
cap gen `check_var'_3=`check_var'^3	
cap gen `check_var'_fxnc=`check_var'*fxnc
}

global full_controls_var1 "age_imb_2 age_imb_3"
global full_controls_var2 "landarea_2 landarea_3"
global full_controls_var3 "pct_male_2 pct_male_3"
global full_controls_var4 "pct_nohs_2 pct_nohs_3"

global outcomes_pick "sd_tot_expgroc_w_"

* RF  STATE FE*
foreach cl in state_fips {
	* Main outcomes RF

			foreach y of global outcomes_pick {
				foreach controls_set in full_controls_var1 full_controls_var2 full_controls_var3 full_controls_var4{				
					forvalues date=21946/22035 {

					cap qui reghdfe `y'`date' fxnc if zipcode_in_sample==1 [aweight=pop], absorb(state_fips one##c.(sd_tot_expgroc_j20 $full_controls1 ${`controls_set'})) cluster(`cl')
					}
			}
		*
		}
*

}
*
}
*











